package com.zhuiYun.project.api.syslog.service.impl;

import com.zhuiYun.project.api.syslog.entity.SysLogException;
import com.zhuiYun.project.api.syslog.model.SysLogExceptionModel;
import com.zhuiYun.project.common.errorCode.CommonException;
import com.zhuiYun.project.common.errorCode.EmErrorCode;
import com.zhuiYun.project.util.pageUtil.MybatisPageHelper;
import com.zhuiYun.project.util.pageUtil.PageRequest;
import com.zhuiYun.project.util.pageUtil.PageResult;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.api.syslog.mapper.SysLogExceptionMapper;
import com.zhuiYun.project.api.syslog.service.SysLogExceptionService;
import com.zhuiYun.project.util.BeanUtils;
import com.zhuiYun.project.util.ObjectUtils;
import com.zhuiYun.project.util.validator.ValidationResult;
import com.zhuiYun.project.util.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 异常日志服务层
 * @auther zcy
 * @data 2023/3/22 13:35
 */
@Service
public class SysLogExceptionServiceImpl  implements SysLogExceptionService {

    @Autowired
    SysLogExceptionMapper sysLogExceptionMapper;

    /**
     * 分页查询异常日志
     * @param pageRequest
     * @param sysLogExceptionModel
     * @return
     */
    @Override
    public PageResult selectSysLogExByPage(PageRequest pageRequest, SysLogExceptionModel sysLogExceptionModel) {
        MybatisPageHelper.startPage(pageRequest);
        return MybatisPageHelper.getPageResult(sysLogExceptionMapper.selectSysLogByPage(sysLogExceptionModel));
    }

    /**
     * 杀出日常日志
     * @param id
     * @return
     * @throws CommonException
     */
    @Override
    public CommonResult deleteSysLogEXById(Integer id) throws CommonException {
        // 判断是否id为空
        if(ObjectUtils.isNotEmpty(id)){
            int i = sysLogExceptionMapper.deleteByPrimaryKey(id);
            // 判断是否操作成功
            if (i <= 0) {
                throw new CommonException(EmErrorCode.DELETE_ERROR);
            }
        }else{
            throw new CommonException(EmErrorCode.ID_NULL);
        }
        return CommonResult.ok(EmErrorCode.DELETE_OK);
    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @Override
    public SysLogException selectSysLogExById(Integer id) throws CommonException {
        // 判断是否id为空
        if(ObjectUtils.isNotEmpty(id)){
            SysLogException sysLogException = sysLogExceptionMapper.selectByPrimaryKey(id);
            // 判断是否操作成功
            if (ObjectUtils.isNotEmpty(sysLogException)) {
                return sysLogException;
            }else{
                throw new CommonException(EmErrorCode.ID_NOT_OBJECT);
            }
        }else{
            throw new CommonException(EmErrorCode.ID_NULL);
        }
    }
    /**
     * 根据时间段查询日志
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public Integer selectSysLogByTime(String startTime,String endTime) {
        Integer integer = sysLogExceptionMapper.selectSysLog(startTime,endTime);
        return integer;
    }
    /**
     * 保存异常日志
     * @param sysLogExceptionModel
     * @return
     * @throws CommonException
     */
    @Override
    public CommonResult saveSysLogEx(SysLogExceptionModel sysLogExceptionModel) throws CommonException {
        if(ObjectUtils.isNotEmpty(sysLogExceptionModel)){
            ValidationResult result = ValidatorUtil.validate(sysLogExceptionModel);
            if (result.isHasError()) {
                throw new CommonException(EmErrorCode.PARAMETER_VALIDATION_ERROR, result.getErrMsg());
            }
            SysLogException sysLogException = new SysLogException();
            // sysLogExceptionModel 对象拷贝成 SysLogException
            BeanUtils.copy(sysLogExceptionModel, sysLogException);
            int i = sysLogExceptionMapper.insert(sysLogException);
            if(i>0){
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.SAVE_ERROR);
            }
        }else{
            // 返回前端异常数据
            throw new CommonException(EmErrorCode.OBJECT_NOT_NULL);
        }
    }
}
